package ru.lukyanets.transmission.client.action;

import com.google.inject.Inject;
import javafx.event.ActionEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import ru.lukyanets.transmission.client.app.PrimaryStage;
import ru.lukyanets.transmission.client.util.DialogUtil;

import com.google.inject.Singleton;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * @author slukyanets
 */
@Singleton
public class AddTorrentFileAction extends AddTorrentAction {
    /** @noinspection unused*/
    @Inject
    @PrimaryStage
    private Stage primaryStage;

    public AddTorrentFileAction() {
        setAccelerator(new KeyCodeCombination(KeyCode.A, KeyCombination.ALT_DOWN));
    }

    @Override
    protected void performAction(ActionEvent event) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("test");
        fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Torrent files", "*.torrent"));
        File file = fileChooser.showOpenDialog(primaryStage);
        if (file != null) {
            try {
                addTorrent(new FileInputStream(file));
            } catch (FileNotFoundException e) {
                DialogUtil.showError("Can't open file");
            }
        }
    }
}
